package com.itboy.mall.pms.product.web;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itboy.common.utils.JsonUtils;
import com.itboy.mall.pms.product.domain.SocialUser;
import com.itboy.mall.pms.product.vo.GiteeVo;
import com.itboy.mall.ums.member.domain.MallMember;
import com.itboy.mall.ums.member.service.IMallMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("web")
public class OAuthController {

	@Autowired
	private IMallMemberService iMallMemberService;

	@GetMapping("giteeLogin")
	public String login(){
		return "login";
	}

	@GetMapping("oauth2.0/gitee/success")
	public String success(@RequestParam("code") String code, HttpSession session){
		Map<String,Object> map=new HashMap<>();
		map.put("grant_type","authorization_code");
		map.put("code",code);
		map.put("client_id","1388e0eb714210a950ca498ce71caaf9d982eb787216e0be59d3c32663fa8e4e");
		map.put("client_secret","95ec1cd72c8d371ba2bb8f87ef29e119331e778885efb32ba409715637c4d71b");
		map.put("redirect_uri","http://127.0.0.1:8085/web/oauth2.0/gitee/success");

		//1.换取accessToken
		String post = HttpUtil.post("https://gitee.com/oauth/token", map);
		SocialUser socialUser = JsonUtils.parseObject(post, SocialUser.class);
		//2.用token换取用户信息
		String giteeString = HttpUtil.get("https://gitee.com/api/v5/user?access_token=" + socialUser.getAccess_token());
		GiteeVo giteeVo = JsonUtils.parseObject(giteeString, GiteeVo.class);
		MallMember mallMember = loginMember(giteeVo, socialUser);
		session.setAttribute("loginUser",mallMember);
		//2.处理响应
		System.out.println(socialUser);
		return "redirect:/";
	}

	private MallMember loginMember(GiteeVo giteeVo,SocialUser socialUser){
		long id = giteeVo.getId();
		MallMember mallMember = iMallMemberService.getOne(new LambdaQueryWrapper<MallMember>().eq(MallMember::getSocialUid, id));
		if(ObjectUtil.isNotNull(mallMember)){
			MallMember update = new MallMember();
			update.setId(mallMember.getId());
			update.setAccessToken(socialUser.getAccess_token());
			update.setExpiresIn(socialUser.getExpires_in());
			iMallMemberService.updateById(update);

			update.setAccessToken(socialUser.getAccess_token());
			update.setExpiresIn(socialUser.getExpires_in());
			return mallMember;
		}else{
			MallMember register = new MallMember();
			register.setNickname(giteeVo.getName());
			register.setHeader(giteeVo.getAvatar_url());
			register.setCreateTime(new Date());
			register.setSocialUid(String.valueOf(giteeVo.getId()));
			register.setAccessToken(socialUser.getAccess_token());
			register.setExpiresIn(socialUser.getExpires_in());

			//把用户信息插入到数据库中
			iMallMemberService.save(register);
			return register;
		}
	}
}
